package com.xonro.common.histrans.zz.biz.base;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.sdk.local.SDK;
import com.alipay.sofa.common.profile.StringUtil;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.zz.biz.common.DrpProcessTransBiz;
import com.xonro.common.histrans.zz.ctx.DrpProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class ProcessBidFileMakeBiz extends DrpProcessTransBiz {
    public  static String  DrpName="";
    public  static String  CCID=DrpConst.ZZ_CCID;
    @Override
    protected void transProcess0(DrpProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        SDK.getLogAPI().consoleInfo("获取标书制作数据");
        List<RowMap> contentData = new ArrayList<>();
        //历史数据迁移映射表
        BO detail = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION").addQuery("PROCESSID=", "obj_22a4d9e14ee1425aaa0f1113da90cde2").detail();
        //历史数据迁移映射子表 主表的
        BO detailed = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_L").addQuery("BINDID=", detail.getBindId()).addQuery("IS_MAIN=", 1).detail();
        DrpName = detailed.getString("DRP_NAME");
        SDK.getLogAPI().consoleInfo("drp表名——+++++"+DrpName);
        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable(detailed.getString("BPM_NAME"));//项目基础信息
        logModel.setOuterTable(DrpName);//基础项目信息表
        logModel.setOuterPK(mainData.getString("MyId"));
        logModel.setOuterExt1(mainData.getString("TeamName"));//团队名称
        logModel.setOuterExt2(mainData.getString("BillNo"));//单据号
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBoName(detailed.getString("BPM_NAME"));
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);

        mainBO.set("DATA_TYPE", "2");//历史数据
        mainBO.set("DRP_ID", mainData.getString("MyId"));
        mainBO.set("APPLY_NO", mainData.getString("BillNo"));
        mainBO.set("APPLY_DATE", mainData.getString("CreateTime"));
        mainBO.set("APPLY_USER", mainData.getString("CreateName"));
        mainBO.set("APPLY_USER_ID", mainData.getString("CreateId"));
        mainBO.set("APPLY_DEPT", mainData.getString("CreateDept"));
        mainBO.set("APPLY_DEPT_ID", mainData.getString("CreateDeptId"));
        //历史迁移子子表 主表的 非附件
        List<BO> list = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", detailed.getId()).addQuery("TYPE!=2", null).list();
        if (list != null && list.size() > 0) {
            SDK.getLogAPI().consoleInfo("/历史迁移子子表 主表的有值+++++");
            for (BO bo : list) {

                SDK.getLogAPI().consoleInfo("bpm++++"+bo.getString("BPM_FIELD"));
                SDK.getLogAPI().consoleInfo("drp字段++++"+bo.getString("DRP_FIELD"));
                SDK.getLogAPI().consoleInfo("字段值++++"+mainData.getString(bo.getString("DRP_FIELD")));
                mainBO.set(bo.getString("BPM_FIELD"), mainData.getString(bo.getString("DRP_FIELD")));
            }
        }
        if (StringUtil.equals("1", detail.getString("SOURCE_DATABASE"))) {
            CCID = DrpConst.ZZ_DRP_1_CCID;
            //历史数据迁移映射子表 子表的
            List<BO> list2 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_L").addQuery("BINDID=", detail.getBindId()).addQuery("IS_MAIN=", 0).list();
            if (list2 != null && list2.size() > 0) {
                for (BO bo : list2) {
                    contentData = SDK.getCCAPI().getRDSAPI(CCID).getMaps("SELECT * FROM " + bo.getString("DRP_NAME") + " WHERE Wiid = ?  ", mainData.getString("Wiid"));
                }
            }
        }
        else {
                //历史数据迁移映射子表 子表的
                List<BO> list1 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_L").addQuery("BINDID=", detail.getBindId()).addQuery("IS_MAIN=", 0).list();
                if (list1 != null && list1.size() > 0) {
                    for (BO bo : list1) {
                        contentData = SDK.getCCAPI().getRDSAPI(CCID).getMaps("SELECT * FROM " + bo.getString("DRP_NAME") + "  WHERE Wiid = ?  ", mainData.getString("Wiid"));
                        for (RowMap content : contentData) {
                            String uuid = UUIDGener.getUUID();
                            BOCreateData subBOCreateData = new BOCreateData();
                            boCreateDataList.add(subBOCreateData);
                            subBOCreateData.setCreateUser(createUser);
                            subBOCreateData.setBoName(bo.getString("BPM_NAME"));
                            BO subBO = new BO();
                            subBO.setId(uuid);
                            subBOCreateData.setBo(subBO);
                            //历史迁移子子表 子表的 非附件
                            List<BO> boList = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", bo.getId()).addQuery("TYPE!=", 2).list();
                            if (boList != null && boList.size() > 0) {
                                for (BO bo1 : boList) {
                                    subBO.set(bo1.getString("BPM_FIELD"), content.getString(bo1.getString("DRP_FIELD")));
                                }
                            }
                             super.addLogModel(historyTransContext, bo.getString("DRP_NAME"), content.getString("id"), bo.getString("BPM_NAME"), uuid, bindId);
                            //历史迁移子子表 子表的 附件
                            List<BO> boList1 = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", bo.getId()).addQuery("TYPE=", 2).list();
                            if (boList1 != null && boList1.size() > 0) {
                                for (BO bos : boList1) {
                                    FileTransData fileTransData = new FileTransData();
                                    fileTransData.setBoId(mainBOId);
                                    //OA表订单编号
                                    //   fileTransData.setFkCode(mainData.getString("xmbh"));
                                    fileTransData.setFileJson(content.getString(bos.getString("DRP_FIELD")));
                                    fileTransData.setProcessInstId(bindId);
                                    historyTransContext.getFileTransDatas().add(fileTransData);
                                    fileTransData.setBoName(bo.getString("BPM_NAME"));
                                    fileTransData.setFieldName(bos.getString("BPM_FIELD"));
                                    // fileTransData.setUserId(mainData.getString("CreateId"));//编制人Id
                                    fileTransData.setUserId("admin");
                                    fileTransData.setAppId("com.awspaas.user.apps.zhongzhuang.mis");
                                    fileTransData.setCreateDate(new Timestamp(mainData.getDate("CreateTime").getTime()));
                                }
                            }
                        }
                    }
                }
            }
            historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);
            //历史数据迁移映射子表 主表的 附件
            List<BO> boList = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_LIST").addQuery("BINDID=", detailed.getId()).addQuery("TYPE=", 2).list();
            if (boList != null && boList.size() > 0) {
                for (BO bo : boList) {
                    SDK.getLogAPI().consoleInfo("附件名称+++"+bo.getString("DRP_FIELD"));
                    FileTransData fileTransData = new FileTransData();
                    fileTransData.setBoId(mainBOId);
                    //OA表订单编号
                    //   fileTransData.setFkCode(mainData.getString("xmbh"));
                    fileTransData.setFileJson(mainData.getString(bo.getString("DRP_FIELD")));
                    fileTransData.setProcessInstId(bindId);
                    historyTransContext.getFileTransDatas().add(fileTransData);
                    fileTransData.setBoName(detailed.getString("BPM_NAME"));
                    fileTransData.setFieldName(bo.getString("BPM_FIELD"));
                    // fileTransData.setUserId(mainData.getString("CreateId"));//编制人Id
                    fileTransData.setUserId("admin");
                    fileTransData.setAppId("com.awspaas.user.apps.zhongzhuang.mis");
                    fileTransData.setCreateDate(new Timestamp(mainData.getDate("CreateTime").getTime()));
                }
            }
    }
    @Override
    protected String getDrpType() {
        return null;
    }

    @Override
    protected String getProcessDefId() {
        return "obj_22a4d9e14ee1425aaa0f1113da90cde2";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_ecdafffecc184cd492bddeeba2059442";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String billNo = ctx.getMainData().getString("BillNo");//单据号
        return "标书制作流程历史数据同步"+billNo;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("Wiid");
    }

    @Override
    protected String getOuterTable(DrpProcessCreateContext historyTransContext) {
        return "ct_BPM_BidProductionApply";
    }

    @Override
    protected String getPk(DrpProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("MyId");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM ct_BPM_BidProductionApply");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}